<?php
/**
 * @user: thanatos <thanatos915@163.com>
 */
namespace frontend\models;

use common\models\Sign;

class SignForm extends \yii\base\Model
{
    public $name;
    public $mobile;
    public $number;

    public function rules()
    {
        return [
            [['name', 'mobile', 'number'], 'required'],
            [['name', 'mobile', 'number'], 'string'],
        ];
    }

    public function submit($params)
    {
        $this->load($params, '');
        if (!$this->validate()) {
            return false;
        }

        // 检查领取个数
        $count = Sign::find()->count(1);
        if ($count >= 666) {
            $this->addError('number', '报名人数已满');
            return false;
        }
        if (Sign::find()->where(['number' => $this->number])->one()) {
            $this->addError('number', '您的号码已经被领取过, 不能重复领取');
            return false;
        }

        $model = new Sign();
        $model->mobile = $this->mobile;
        $model->name = $this->name;
        $model->number = $this->number;
        $model->sort = $count ?: 0 + 1;
        $model->created_at = time();
        if ($model->save()) {
            return $model;
        } else {
            $this->addError('number', '服务器错误');
        }

    }

}